package com.zhihu_lvliangi_android.option.zhengwu.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.JsonPageModel;
import com.zhihu_lvliangi_android.domain.ZhengwuColumnItem;
import com.zhihu_lvliangi_android.domain.ZhengwuGuideItem;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.zhengwu.AffairDetailActivity;
import com.zhihu_lvliangi_android.option.zhengwu.Category_Item_List_Activity;
import com.zhihu_lvliangi_android.option.zhengwu.adapter.ColumnGridViewAdapter;
import com.zhihu_lvliangi_android.service.MobileApplication;
import com.zhihu_lvliangi_android.util.Constant;
import com.zhihu_lvliangi_android.util.RequestURL;

public class Category_Theme_items extends ParentFragment {
	
	private int mPosition;
	private ArrayList<ZhengwuColumnItem> mColumnItems;

	public static Category_Theme_items newInstance(int pos) {
		Category_Theme_items items = new Category_Theme_items();
		Bundle bundle = new Bundle();
		bundle.putInt(Constant.GENERAL_SAVE_KEY, pos);
		items.setArguments(bundle);
		//初始化数据
		return items;
	}
	
	public static Category_Theme_items newInstance(List<ZhengwuColumnItem> list) {
		Category_Theme_items items = new Category_Theme_items();
		Bundle bundle = new Bundle();
		bundle.putSerializable(Constant.GENERAL_SAVE_KEY, (Serializable) list);
		items.setArguments(bundle);		
		return items;
	}

	public int getPosition() {
		return mPosition;
	}
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setUseMain_scrollview(false);
		super.onCreate(savedInstanceState);
//		mPosition = getArguments().getInt(Constant.GENERAL_SAVE_KEY);
		mColumnItems = (ArrayList<ZhengwuColumnItem>) getArguments().getSerializable(Constant.GENERAL_SAVE_KEY);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_theme_subitems;
	}

	@Override
	protected void setupViews(View parentView) {
		FrameLayout frameLayout = (FrameLayout) parentView.findViewById(R.id.main_dialog_frame);
		frameLayout.setBackgroundResource(R.drawable.main_option_dialog);
		GridView gridView = (GridView) parentView.findViewById(R.id.theme_items_gridview);
		ColumnGridViewAdapter adapter = new ColumnGridViewAdapter(getActivity(),mColumnItems);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(itemClickListener);
		
	}

	@Override
	protected void initialized(Bundle savedInstanceState) {

	}

	@Override
	public void threadTask() {

	}
	
	private OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			//查询
			Gson gson = new Gson();
			try {
				
				String jsonString =MobileApplication.cacheUtils.getAsString(mColumnItems.get(position).getGuide_type_id());
				if (TextUtils.isEmpty(jsonString)) {
					Log.e(MobileApplication.TAG
							, "jsonString 从array 中取");
					jsonString = getResources().getString(R.string.json_item);
				}
				//TODO
				Intent intent = new Intent(getActivity(), Category_Item_List_Activity.class);
				intent.putExtra(Constant.GENERAL_SAVE_KEY, jsonString);
				intent.putExtra("URL", RequestURL.getGuideItemByTypeID(mColumnItems.get(position).getGuide_type_id()));
				startActivity(intent);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	


}
